package weaver.formmode.customjavacode.modeexpand.xfjt.duss;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.RequestService;
import weaver.workflow.request.RequestComInfo;
import weaver.workflow.workflow.WorkflowComInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-06-13
 * @Description: 发送试卷
 * @ModifyLog:
 **/

public class WeaSendTestExpand extends AbstractModeExpandJavaCodeNew {
    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            //数据id
            int billid = -1;
            //模块id
            int modeid = -1;
            //模块id
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if (billid > 0 && modeid > 0) {
                    String curdate = DateUtil.getCurrentDate();
                    RecordSet rs = new RecordSet();
                    RecordSet rs1 = new RecordSet();
                    try {
                        ResourceComInfo comInfo = new ResourceComInfo();
                        rs.executeQuery("select * from uf_sjtz where id=?",billid);
                        if(rs.next()){
                            String cjr = Util.null2String( rs.getString("cjr") );
                            String cjrq = Util.null2String( rs.getString("cjrq") );
                            String sjmc = Util.null2String( rs.getString("sjmc") );
                            String ktszlx = Util.null2String( rs.getString("ktszlx") );
                            String yjr = Util.null2String( rs.getString("yjr") );
                            String ksscfz = Util.null2String( rs.getString("ksscfz") );
                            String kskssj = Util.null2String( rs.getString("kskssj") );
                            String ksjssj = Util.null2String( rs.getString("ksjssj") );
                            String zf = Util.null2String( rs.getString("zf") );
                            String jgfs = Util.null2String( rs.getString("jgfs") );
                            String cjksry = Util.null2String( rs.getString("cjksry") );
                            String [] arr = cjksry.split(",");
                            int count = 0;
                            for (int i = 0; i <arr.length ; i++) {
                                String userid = arr[i];
                                String username = comInfo.getLastname(userid);
                                String requestname ="在线考试答题-"+username+"-"+curdate;
                                Map<String, String> requestMainMap = new HashMap<>(  );
                                requestMainMap.put( "dtr",userid );
                                requestMainMap.put( "cjr",cjr );
                                requestMainMap.put( "gs",comInfo.getSubCompanyID(userid) );
                                requestMainMap.put( "bm",comInfo.getDepartmentID(userid) );
                                requestMainMap.put( "cjrq",cjrq );
                                requestMainMap.put( "sjmc",sjmc );
                                requestMainMap.put( "ktszlx",ktszlx );
                                requestMainMap.put( "yjr",yjr );
                                requestMainMap.put( "kssc",ksscfz );
                                requestMainMap.put( "kskssj",kskssj );
                                requestMainMap.put( "ksjssj",ksjssj );
                                requestMainMap.put( "zf",zf );
                                requestMainMap.put( "jgfs",jgfs );
                                requestMainMap.put( "cjksry",cjksry );
                                requestMainMap.put( "jmsjid",billid+"" );
                                String requestid = WorkflowCreateByRequestMap(userid,"133",requestname,"0",requestMainMap);
                                if(Util.getIntValue(requestid)>0){
                                    rs.executeQuery("select id from formtable_main_286 where requestid=?",requestid);
                                    rs.next();
                                    String mainid = Util.null2String(rs.getString("id"));
                                    String [] strs = new String[]{"1","2","3","4"};
                                    for (int j = 0; j <strs.length ; j++) {
                                        rs1.executeQuery("select tm,fz,px from uf_sjtz_dt"+strs[j]+" where mainid=?",billid);
                                        while (rs1.next()){
                                            String tm = Util.null2String(rs1.getString("tm"));
                                            String fz = Util.null2String(rs1.getString("fz"));
                                            String px = Util.null2String(rs1.getString("px"));
                                            rs1.executeUpdate("insert into formtable_main_286_dt"+strs[j]+" (mainid,tm,fz,px) values(?,?,?,?)",mainid,tm,fz,px);
                                        }
                                    }
                                    count++;
                                }
                            }
                            boolean flag = arr.length == count;
                            if (flag){
                                rs.executeUpdate("update uf_sjtz set sfyfs=0,sjzt=1 where id=?",billid);
                            }
                        }

                    }catch (Exception e){
                        e.printStackTrace();
                    }/*
                     *@Description:
                     */

                }
            }
        } catch (Exception e) {
            result.put("errmsg", "自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }

    //创建流程方法
    public String WorkflowCreateByRequestMap(String creatorId, String workflowId, String requestName, String IsNextFlow, Map<String, String> requestMainMap) {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog( "-----创建流程传递的参数个数--------" );
        baseBean.writeLog( "creatorId:" + creatorId );
        baseBean.writeLog( "workflowId:" + workflowId );
        baseBean.writeLog( "requestName:" + requestName );
        baseBean.writeLog( "IsNextFlow:" + IsNextFlow );

        String requestid = "";
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setCreatorid( creatorId );//创建人Id
        requestInfo.setWorkflowid( workflowId );//工作流Id
        requestInfo.setDescription( requestName );//请求标题
        if ("0".equals( IsNextFlow )) {
            requestInfo.setIsNextFlow( IsNextFlow );
        }

        //主表字段
        MainTableInfo mainTableInfo = new MainTableInfo();
        Property[] propertyArray = new Property[requestMainMap.size()];
        int p = 0;
        for (Map.Entry<String, String> entry : requestMainMap.entrySet()) {
            propertyArray[p] = new Property();
            propertyArray[p].setName( Util.null2String( entry.getKey() ) );
            propertyArray[p].setValue( Util.null2String( entry.getValue() ) );
            baseBean.writeLog( "\r\n---p=" + p + "---Key=" + entry.getKey() + "---Value=" + entry.getValue() );
            p++;
        }
        baseBean.writeLog( "-----创建流程传递的参数个数p=" + p );
        mainTableInfo.setProperty( propertyArray );
        requestInfo.setMainTableInfo( mainTableInfo );
        RequestService service = new RequestService();
        try {
            //流程
            try {
                requestid = service.createRequest( requestInfo );//创建请求id
            } catch (Exception e) {
                baseBean.writeLog( "=======" + e.getMessage() );
            }
            //String userId = requestInfo.getLastoperator();//请求最后的操作者
            //记录日志
            StringBuffer sbf = new StringBuffer( "\r\n-----xwd创建工作流记录日志开始" );
            WorkflowComInfo wfcif = new WorkflowComInfo();
            RequestComInfo rcif = new RequestComInfo();
            ResourceComInfo rscif = new ResourceComInfo();
            sbf.append( "\r\n-----姓名:" + rscif.getLastname( rcif.getRequestCreater( requestid ) ) );
            sbf.append( "\r\n-----时间:" + rcif.getRequestCreateTime( requestid ) );
            sbf.append( "\r\n-----创建流程:" + wfcif.getWorkflowname( workflowId ) );
            sbf.append( "\r\n-----请求:" + rcif.getRequestname( requestid ) );
            sbf.append( "\r\n-----请求:" + requestid );
            sbf.append( "\r\n-----创建工作流记录日志结束" );
            baseBean.writeLog( sbf.toString() );
        } catch (Exception e) {
            baseBean.writeLog( "错误：" + e );
        }
        return requestid;
    }

}
